{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "import torch\n",
    "import numpy as np\n",
    "import yaml\n",
    "from tqdm import tqdm\n",
    "from PIL import Image\n",
    "import glob\n",
    "\n",
    "from utils_func import *\n",
    "from sample_func import *\n",
    "from colorizer import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Working with z of shape (1, 1024, 16, 16) = 262144 dimensions.\n"
     ]
    }
   ],
   "source": [
    "# Coco model\n",
    "ckpt_file = '/home/huangzhitong/code/unicolor/framework/logs/bert_final/epoch=14-step=142124_new.ckpt'\n",
    "device = 'cuda:0'\n",
    "colorizer = Colorizer(ckpt_file, device, [256, 256], load_clip=False, load_warper=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Colorize COCO val set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:03<00:00,  3.45s/it]\n"
     ]
    }
   ],
   "source": [
    "org_path = '/home/huangzhitong/code/unicolor/results/test/original'\n",
    "col_path = '/home/huangzhitong/code/unicolor/results/test/colorized'\n",
    "\n",
    "os.makedirs(org_path, exist_ok=True)\n",
    "os.makedirs(col_path, exist_ok=True)\n",
    "\n",
    "img_paths = glob.glob('/home/huangzhitong/dataset/coco/val2017/*.jpg')\n",
    "\n",
    "for path in tqdm(img_paths[1011:1012]):\n",
    "    img = Image.open(path).convert('RGB')\n",
    "    gray = img.convert('L')\n",
    "    colorized = colorizer.sample(gray, [], topk=100)\n",
    "    name = path.split('/')[-1].split('.')[0]\n",
    "    img.save(os.path.join(org_path, name+'.png'))\n",
    "    colorized.save(os.path.join(col_path, name+'.png'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Update old state keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "ckpt_file = '/home/huangzhitong/code/unicolor/framework/logs/bert_final/epoch=14-step=142124.ckpt'\n",
    "ckpt = torch.load(ckpt_file)\n",
    "state = ckpt['state_dict']\n",
    "for key in list(state.keys()):\n",
    "    state[key.replace('hybrid_vqgan', 'chroma_vqgan').replace('coltran', 'hybrid_tran')] = state.pop(key)\n",
    "torch.save(ckpt, ckpt_file.replace('.ckpt', '_new.ckpt'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.13 ('unicolor')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d2a626b74e299e13c7ec875d6c47b0f8e040df285fc56a1acf5a98c054a62f0a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
